package com.opennews.openplatform.websocket

import groovy.transform.CompileStatic
import org.springframework.messaging.handler.annotation.MessageMapping
import org.springframework.messaging.simp.SimpMessageSendingOperations
import org.springframework.stereotype.Controller

/**
 * @Controller annotation and spring bean registration in conf/spring/resource.groovy are VERY IMPORTANT HERE!!!
 * Even we do not treat this class as a mvc controller we still need this annotation.
 * Otherwise client message will NOT be received.
 */
@CompileStatic
@Controller
class WebSocketHandler {
    SimpMessageSendingOperations brokerMessagingTemplate

    /**
     * This is an simple example that demonstrates how to use @SendTo annotation.
     * @param world
     */
    // @MessageMapping("/hello")
    // @SendTo("/topic/greetings")
    // String hello(String world) {
    //     return "hello, ${world}!"
    // }

    @MessageMapping("/hello")
    void hello(WebSocketBody body) {
        brokerMessagingTemplate.convertAndSend(body.clientOnDestination, [
            timestamp: new Date().toTimestamp(),
            msg: body.payload,
        ])
    }
}